<?php

namespace app\common\command;

use addons\shopro\service\Wallet as WalletService;
use app\admin\model\Children;
use app\admin\model\market\Acceleration;
use app\admin\model\shopro\order\Order;
use app\admin\model\shopro\order\OrderItem;
use app\admin\model\shopro\user\WalletLog;
use app\common\model\shopro\ReleaseLog;
use app\common\model\shopro\ShoproPerformanceDayfenModel;
use app\common\model\shopro\ShoproPerformanceModel;
use app\common\model\shopro\UserReleaseModel;
use app\common\model\User;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;
use think\exception\HttpResponseException;

class Jixiao extends Command
{
    protected function configure()
    {
        $this->setName('jixiao')->setHelp('绩效奖')->setDescription('绩效奖');
    }

    protected function execute(Input $input, Output $output)
    {
        $model           = new ShoproPerformanceModel();
        $releaseLogModel = new ReleaseLog();
        $data            = $model->with([
            'user' => function ($query) {
                $query->field('id,a_all_money,b_all_money,score,level');
            }
        ])->where(['status'=>0])->select();
//        dump($data);exit();
        if ($data) {
            $jixiao_bili  = config('site.jixiao_bili') / 100;
            $jixiao_rijie = config('site.jixiao_rijie');
            $golen_scale  = config('site.golen_scale') / 100;        //金币比例
            $slive_scale  = (100 - config('site.golen_scale')) / 100;//银币比例
            $jixiao_day   = config('site.jixiao_day');
            $dayFenModel  = new ShoproPerformanceDayfenModel();
            $userReleaseModel = new UserReleaseModel();
            $all_golden   = 0;
            $all_silver   = 0;
            $all_score    = 0;
            $user_num     = 0;
            $user_ids     = '';
            Db::startTrans();
            try {
                foreach ($data as $k => $v) {
                    if($v['user']){
                        $a_money = bcadd($v['a_money'], $v['user']['a_all_money'], 2);
                        $b_money = bcadd($v['b_money'], $v['user']['b_all_money'], 2);
                        if ($a_money > 0 && $b_money > 0) {
                            if ($a_money == $b_money) {
                                $release              = $a_money;
                                $v->user->a_all_money = 0;
                                $v->user->b_all_money = 0;
                            } elseif ($a_money > $b_money) {
                                $release              = $b_money;
                                $jieyu                = $a_money - $b_money;
                                $v->user->a_all_money = bcadd($v->user->a_all_money, $jieyu);
                                $v->user->b_all_money = 0;
                            } else {
                                $release              = $a_money;
                                $jieyu                = $b_money - $a_money;
                                $v->user->a_all_money = 0;
                                $v->user->b_all_money = bcadd($v->user->b_all_money, $jieyu);
                            }
//                            dump($release);
                            $release = bcmul($release,'0.1',2);
//                            dump($release);
                            $eliminate_max = Acceleration::where(['id'=>$v->user->level])->value('eliminate_max');
                            if($eliminate_max){
                                if($release >= $eliminate_max){
                                    $release = $eliminate_max;
                                }
                                if($v->user->score > 0){
                                    if ($release >= $v->user->score) {
                                        $release = $v->user->score;
                                    }
                                    $user_num++;
                                    $all_score     += $release;
                                    $user_ids      .= ','.$v->user_id;
                                    $gold          = bcmul($release, $golen_scale, 2);
                                    $all_golden    += $gold;
                                    $slive         = bcmul($release, $slive_scale, 2);
                                    $all_silver    += $slive;
                                    if ($jixiao_rijie == 1) {
                                        WalletService::change($v->user_id, 'score', -$release, 'release_jixiao',
                                            ['id' => $v->id],
                                            '绩效奖释放','',$v->user->level);
                                        WalletService::change($v->user_id, 'gold', $gold, 'release_gold_jixiao',
                                            ['id' => $v->id],
                                            '绩效奖');
                                        WalletService::change($v->user_id, 'silver', $slive, 'release_slive_jixiao',
                                            ['id' => $v->id],
                                            '绩效奖');
                                    } else {
                                        $release = $release / $jixiao_day;
                                        for ($i = 1; $i <= $jixiao_day; $i++) {
                                            $dayFenInfo = $dayFenModel->where([
                                                'user_id'        => $v->user_id,
                                                'performance_id' => $v->id
                                            ])->find();
                                            if (!$dayFenInfo) {
                                                $end_time = strtotime('+'.$i.' day', time());
                                                $dayFenModel->create([
                                                    'user_id'  => $v->user_id,
                                                    'release'  => $release,
                                                    'end_time' => $end_time
                                                ]);
                                            }
                                        }
                                    }
                                    $r = $v->user->save();
                                }
                            }
                        } else {
                            if ($a_money) {
                                $v->user->a_all_money = bcadd($v->user->a_all_money, $a_money);
                            }
                            if ($b_money) {
                                $v->user->b_all_money = bcadd($v->user->b_all_money, $b_money);
                            }
                            $r = $v->user->save();
                        }
                    }
                    $v->status = 1;
                    $v->save();
                }
                Db::commit();
            } catch (HttpResponseException $e) {
                Db::rollback();
                $data    = $e->getResponse()->getData();
                $message = $data ? ($data['msg'] ?? '') : $e->getMessage();
                echo $message;
            }
            if ($r) {
                $releaseLogModel->user_ids   = trim($user_ids, ',');
                $releaseLogModel->user_num   = $user_num;
                $releaseLogModel->all_score  = $all_score;
                $releaseLogModel->all_golden = $all_golden;
                $releaseLogModel->all_silver = $all_silver;
                $releaseLogModel->type       = 2;
                $releaseLogModel->save();
            }
        }
    }
}
